package sy.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.OrderDrug;
import sy.service.OrderDrugServiceI;
import sy.util.StringUtil;

@Controller
@RequestMapping("/orderDrugController")
public class OrderDrugController extends BaseController {
	
	@Autowired
	private OrderDrugServiceI orderDrugService;
	
	@RequestMapping("/datagrid")
	@ResponseBody
	public DataGrid datagrid(OrderDrug order) throws Exception {
		return orderDrugService.datagrid(order);
	}
	
	@RequestMapping("/datagrid/accepted")
	@ResponseBody
	public DataGrid datagridForAccepted(OrderDrug order) throws Exception {
		return orderDrugService.datagridForAccepted(order);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(OrderDrug order, HttpSession session) {
		Json j = new Json();
		try {
			OrderDrug result = orderDrugService.add(order);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(result);
			
			//add log
			logUserAction(session, "/orderDrugController/add", result.toString());
			
		} catch (Exception e) {
			j.setMsg("添加失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(OrderDrug order, HttpSession session) {
		Json j = new Json();
		try {
			OrderDrug result = orderDrugService.editDrugSave(order);
			j.setSuccess(true);
			j.setMsg("保存成功！");
			logUserAction(session, "/orderDrugController/edit", result.toString());
		} catch (Exception e) {
			j.setMsg("保存失败：" + e.getMessage());
		}
		return j;
	}


	@RequestMapping("/save_or_submit")
	@ResponseBody
	public Json edit_ok(OrderDrug order, HttpSession session) {
		Json j = new Json();
		try {
			OrderDrug result = orderDrugService.edit(order);
			j.setSuccess(true);
			j.setMsg("修改成功！");
			logUserAction(session, "/orderDrugController/edit_ok", result.toString());
		} catch (Exception e) {
			j.setMsg("修改失败：" + e.getMessage());
		}
		return j;
	}

	@RequestMapping("/cancel")
	@ResponseBody
	public Json cancel(OrderDrug order, HttpSession session) {
		Json j = new Json();
		try {
			OrderDrug result = orderDrugService.editOrderStatus(order);
			j.setSuccess(true);
			j.setMsg("取消成功！");
			logUserAction(session, "/orderDrugController/cancel", result.toString());
		} catch (Exception e) {
			j.setMsg("取消失败：" + e.getMessage());
		}
		return j;
	}


	@RequestMapping("/item")
	@ResponseBody
	public Json getDrugItemsByOrderId(String orderId) {
		Json j = new Json();
		try {
			System.out.println(orderId);
			if(StringUtil.isNullOrEmpty(orderId)) {
				throw new Exception("订单号为空！");
			}
			OrderDrug order = orderDrugService.getDrugItemsByOrderId(orderId);
			j.setSuccess(true);
			j.setObj(order);
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("获取订单信息失败：" + e.getMessage());
		}
		return j;
	}
	
	@RequestMapping("/addDetails")
	@ResponseBody
	public Json addDetails(OrderDrug order, HttpSession session) {
		Json j = new Json();
		try {
			OrderDrug result = orderDrugService.addDetails(order);
			j.setSuccess(true);
			j.setMsg("生成详单成功！");
			//j.setObj(result);
			
			//add log
			logUserAction(session, "/orderDrugController/addDetails", result.toString());
			
		} catch (Exception e) {
			j.setMsg("生成详单失败：" + e.getMessage());
		}
		return j;
	}

}